import requests
from core import settings
from lib import conf_common
from jsonpath import jsonpath

"""采购商登录"""


class ShoppingMallCommonLogin:
    """获取配置文件信息"""
    base = conf_common.UseCofg(settings.Cfg_path)

    """登录"""

    @classmethod
    def select_company_login(cls):
        """url"""
        select_company_login_url = cls.base.use_cofg("base_address", "base_address_url") + ('/mall-gateway-test/mall'
                                                                                            '-user/mall-user/auth'
                                                                                            '/loginCompanyList')
        select_company_header = eval(cls.base.use_cofg("headers", "headers"))
        select_company_params = {"days": 1,
                                 "loginMode": "",
                                 "phone": cls.base.use_cofg("user", "shopping_phone"),
                                 "smsCode": cls.base.use_cofg("user", "identifying_code"),
                                 "isSelect": ""
                                 }
        response = requests.post(url=select_company_login_url, json=select_company_params,
                                 headers=select_company_header)
        res = response.json()
        """获取公司code"""
        code = jsonpath(res, "$..code")[-2]
        cls.select_company_code = code

    """登录选择公司"""

    @classmethod
    def selest_login(cls):
        """获取url"""
        select_url = cls.base.use_cofg("base_address", "base_address_url") + ("/mall-gateway-test/mall-user/mall-user"
                                                                              "/auth/selectLogin")
        """请求头"""
        select_headers = eval(cls.base.use_cofg("headers", "headers"))

        """参数"""
        select_params = {"companyCode": cls.select_company_code,
                         "days": 1,
                         "loginMode": "2",
                         "phone": cls.base.use_cofg("user", "shopping_phone"),
                         "smsCode": cls.base.use_cofg("user", "identifying_code")
                         }

        response = requests.post(url=select_url, json=select_params, headers=select_headers)
        res = response.json()
        # """获取登录token"""
        token = jsonpath(res,"$..loginToken")[0]
        cls.login_token = token
        print(cls.login_token)



